package cn.edu.xmist;

import cn.edu.xmist.pojo.Student;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class HashMapDemo {

    private static Map<Integer, Student> studentMap = new HashMap<>();

    /**
     * 增加学生
     *
     * @param student
     * @return
     */
    public static boolean addStudent(Student student) {
        if (studentMap.containsValue(student.getStuNo())) {
            return Boolean.FALSE;
        } else {
            studentMap.put(student.getStuNo(), student);
            return Boolean.TRUE;
        }
    }

    /**
     * 删除学生
     */
    public static boolean delStudentByNo(int stuNo) {
        if (studentMap.containsValue(stuNo)) {
            studentMap.remove(stuNo);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 修改学生
     */
    public static boolean updateStudent(Student student) {
        if (studentMap.containsValue(student.getStuNo())) {
            studentMap.put(student.getStuNo(), student);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    /**
     * 查询所有学生
     */

    private static Map<Integer, Student> queryAllStudents() {
        return studentMap;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Boolean flag = Boolean.TRUE;
        do {
            System.out.println("===学生管理系统===");
            System.out.println("1.增加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("0.退出系统");

            int choice = input.nextInt();

            switch (choice) {
                case 1:
                    System.out.println("请输入学号：");
                    int stuNo = input.nextInt();
                    System.out.println("请输入姓名：");
                    String name = input.next();
                    System.out.println("请输入年龄：");
                    int age = input.nextInt();
                    System.out.println("请输入身份证号：");
                    String idNumber = input.next();

                    Student student = new Student(stuNo, age, name, idNumber);
                    boolean result = addStudent(student);
                    if (result) {
                        System.out.println("新增学生信息成功！");
                    } else {
                        System.out.println("新增学生信息失败！");
                    }
                    break;
                case 2:
                    System.out.println("请输入学号:");
                    stuNo = input.nextInt();

                    result = delStudentByNo(stuNo);
                    if (result) {
                        System.out.println("删除学生信息成功");
                    } else {
                        System.out.println("删除学生信息失败");
                    }
                    break;
                case 3:
                    System.out.println("请输入学号:");
                    stuNo = input.nextInt();
                    System.out.println("请输入姓名:");
                    name = input.next();
                    System.out.println("请输入年龄:");
                    age = input.nextInt();
                    System.out.println("请输入身份证号:");
                    idNumber = input.next();

                    student = new Student(stuNo, age, name, idNumber);
                    boolean result2 = addStudent(student);
                    if (result2) {
                        System.out.println("修改学生信息成功！");
                    } else {
                        System.out.println("修改学生信息失败！");
                    }
                    break;
                case 4:
                    Map<Integer, Student> allStudent = queryAllStudents();

                    if (allStudent != null) ;
                    System.out.println("学号\t\t姓名\t\t年龄\t\t身份证号");
                    StringBuffer massageSb = new StringBuffer();
                    Set<Integer> keySet = allStudent.keySet();
                    for (Integer no : keySet) {
                        Student stu = allStudent.get(no);
                        massageSb.append(stu.getStuNo());
                        massageSb.append("\t\t\t");
                        massageSb.append(stu.getName());
                        massageSb.append("\t\t\t");
                        massageSb.append(stu.getAge());
                        massageSb.append("\t\t\t");
                        massageSb.append(stu.getIdNumber());
                        massageSb.append("\t\t\t");
                        System.out.println(massageSb);
                        System.out.println("<-------------------------------------------->");
                    }

            break;
            case 0:
                System.out.println("谢谢使用本系统，欢迎下次光临！");
                flag = Boolean.FALSE;
                break;
            default:
                System.out.println("请认真思考一下");
                flag = Boolean.TRUE;
                break;
            }
        }while (flag);
    }
}